<template>
  <a-dropdown v-if="currentUser && currentUser.name" placement="bottomRight">
    <span class="ant-pro-account-avatar">
      <a-avatar size="small" :src="currentUser.avatar != ''? currentUser.avatar : 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png'" class="antd-pro-global-header-index-avatar" />
      <span>{{ currentUser.name }}</span>
    </span>
    <template v-slot:overlay>
      <a-menu class="ant-pro-drop-down menu" :selected-keys="[]">
<!--        <a-menu-item v-if="menu" key="center" @click="handleToCenter">-->
<!--          <a-icon type="user" />-->
<!--          {{ $t('menu.account.center') }}-->
<!--        </a-menu-item>-->
        <a-menu-item v-if="menu" key="settings" @click="handlePool">
          <a-icon type="setting" />
          资源公共池
        </a-menu-item>
        <a-menu-divider v-if="menu" />
        <a-menu-item v-if="menu" key="settings" @click="handleScreen">
          <a-icon type="setting" />
          屏蔽号段池
        </a-menu-item>
        <a-menu-divider v-if="menu" />
        <a-menu-item key="logout" @click="handleLogout">
          <a-icon type="logout" />
          退出登录
        </a-menu-item>
      </a-menu>
    </template>
  </a-dropdown>
  <span v-else>
    <a-spin size="small" :style="{ marginLeft: 8, marginRight: 8 }" />
  </span>
</template>

<script>
import { Modal } from 'ant-design-vue'

export default {
  name: 'AvatarDropdown',
  props: {
    currentUser: {
      type: Object,
      default: () => null
    },
    menu: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    handleToCenter () {
      this.$router.push({ path: '/account/center' })
    },
    // handleToSettings () {
    //   this.$router.push({ path: '/account/settings' })
    // },
    handleLogout (e) {
      Modal.confirm({
        title: '消息',
        content: '是否确定取消登陆',
        okType: 'danger',
        onOk: () => {
          // return new Promise((resolve, reject) => {
          //   setTimeout(Math.random() > 0.5 ? resolve : reject, 1500)
          // }).catch(() => console.log('Oops errors!'))
          return this.$store.dispatch('Logout').then(() => {
            window.location.reload()
          })
        },
        onCancel () {
          this.$router.push({ path: '/account/center' })
        }
      })
    },
    handlePool () {
      window.open('/pool/list')
    },
    handleScreen () {
      window.open('/pool/list2')
    }
  }
}
</script>

<style lang="less" scoped>
.ant-pro-drop-down {
  /deep/ .action {
    margin-right: 8px;
  }
  /deep/ .ant-dropdown-menu-item {
    min-width: 160px;
  }
}
</style>
